﻿using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Raffles.OA.Utility
{
    public class EmailHelper
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="Host">
        /// 腾讯邮箱：smtp.exmail.qq.com
        /// 163邮箱：smtp.163.com
        /// </param>
        /// <param name="Port">
        /// 腾讯邮箱：587
        /// 163邮箱：25
        /// </param>
        /// <param name="UseDefaultCredentials">
        /// false
        /// </param>
        /// <param name="EnableSsl">
        /// 腾讯邮箱：true
        /// 163邮箱：false
        /// </param>
        /// <param name="userName">
        /// 腾讯邮箱：jeff.cao@srida-info.com
        /// 163邮箱：wise_cao@163.com
        /// </param>
        /// <param name="password">
        /// 腾讯邮箱：mEwhJL29cF2UHFKJ
        /// 163邮箱：YRJHHXJDLOKNJIST
        /// </param>
        /// <param name="mail"></param>
        public static void MailSend(string Host, int Port,
            bool UseDefaultCredentials, bool EnableSsl,
            string userName, string password,
            MailMessage mail)
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = Host;
            smtp.Port = Port;
            smtp.UseDefaultCredentials = UseDefaultCredentials;
            smtp.EnableSsl = EnableSsl;
            smtp.Credentials = new NetworkCredential(userName, password);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(mail);
        }

        /// <summary>
        /// 生成邮件
        /// </summary>
        /// <param name="From">jeff.cao@srida-info.com</param>
        /// <param name="To">wise_cao@163.com</param>
        /// <param name="Subject">您有待分配的报修申请</param>
        /// <param name="Body">
        /// <a href="http://localhost:23188/Customer/Repair/Create/mSXD" target="_blank">您有待分配的报修申请，请尽快处理！</a>
        /// </param>
        /// <param name="IsBodyHtml">true</param>
        /// <param name="BodyEncoding">Encoding.UTF8</param>
        /// <param name="Priority">MailPriority.High</param>
        /// <returns></returns>
        public static MailMessage MailMessage(string From, string To,
            string Subject, string Body,
            bool IsBodyHtml, Encoding BodyEncoding, MailPriority Priority)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(From);
            var toList = GetToList(To);
            foreach (string toAddress in toList)
            {
                if (string.IsNullOrWhiteSpace(toAddress))
                {
                    continue;
                }
                mail.To.Add(toAddress.Trim());
            }
            mail.Subject = Subject;
            mail.Body = Body;
            mail.IsBodyHtml = IsBodyHtml;
            mail.BodyEncoding = BodyEncoding;
            mail.Priority = Priority;
            return mail;
        }

        /// <summary>
        /// 获取收件人列表
        /// </summary>
        /// <param name="to">字符串形式</param>
        /// <returns>列表形式</returns>
        public static List<string> GetToList(string to)
        {
            var toList = to.Split(';');
            var result = new List<string>();
            foreach (string toAddress in toList)
            {
                if (string.IsNullOrWhiteSpace(toAddress))
                {
                    continue;
                }
                result.Add(toAddress);
            }
            return result;
        }

        #region 发送邮件

        /// <summary>
        /// 是否邮件通知
        /// </summary>
        public static bool IsMailNotice => CacheHelper.GetCache("IsMailNotice").ToBool();

        /// <summary>
        /// 平台邮箱Host
        /// </summary>
        public static string MailHost => CacheHelper.GetCache("MailHost").ToString();

        /// <summary>
        /// 平台邮箱Port
        /// </summary>
        /// <returns></returns>
        public static int MailPort => CacheHelper.GetCache("MailPort").ToInt();

        /// <summary>
        /// 平台邮箱地址
        /// </summary>
        public static string MailAddress => CacheHelper.GetCache("MailAddress").ToString();

        /// <summary>
        /// 平台邮箱授权码
        /// </summary>
        public static string MailPassword => CacheHelper.GetCache("MailPassword").ToString();

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toUser">收件人</param>
        /// <param name="message">邮件内容</param>
        public static void SendMailMessage(string toUser, string subject, string content)
        {
            if (IsMailNotice)
            {
                var mail = EmailHelper.MailMessage(MailAddress, toUser, subject, content, true, Encoding.UTF8, MailPriority.High);
                EmailHelper.MailSend(MailHost, MailPort, false, true, MailAddress, MailPassword, mail);
            }
        }
        #endregion 
    }
}